package cn.ulyn.seckill.main;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.File;
import java.net.URLDecoder;

public class Main {

    public static void main(String[] a) throws Exception {
        start(8100);
//        long now = 1408118400263L;
//        Date date = new Date(now);
//        int sec = date.getSeconds();
//        System.out.println(sec);
//        String nowStr =  Long.toString(now);
//        final int sleepTime = 2900 - (sec * 1000 + Integer.parseInt(nowStr.substring(nowStr.length()-4)));
//        System.out.println(sleepTime);
//        String tip = "\u86CB\u529B\u88D9\u9634\u67D1\u832C\u75B5\u6B64\u6402\u74DC\u5B55\u5987\u7B5B\u5FD7\u7B94\u9884";
//        String decodeTip = new String(tip.getBytes(),"utf-8");
//        System.out.println(decodeTip);
//
//        String an = "孕妇装";
//        //去掉提示的字中没有的
//        List<String> anChars = new ArrayList<String>();
//        for(int i=0,l=an.length();i<l;i++){
//            String s = an.substring(i, i + 1);
//            if(decodeTip.indexOf(s)!=-1){
//                anChars.add(s);
//            }
//        }
//        StringBuilder sb = new StringBuilder();
//        for(String s : anChars){
//            sb.append(s);
//        }
//        an = sb.toString();
//        System.out.println(an);
    }

    public static void start(int port) throws Exception {

        String webapp = "E:\\workplace\\淘宝\\ulyn-seckill\\src\\main\\webapp";


        if (port == 0) {
            port = 8099;
        }

        Server server = new Server(port);

        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        context.setResourceBase(getPath(webapp));
        //context.setWar("E:/share/test/struts2-blank.war");
        server.setHandler(context);
        server.start();
    }

    public static String getPath(String webapp) {
        String classPath = null;
        try {
            classPath = getClassPath();
            if (classPath.endsWith("classes/")) {
                classPath = webapp;
            } else {
                classPath = classPath.substring(0, classPath.length() - 4) + "webapp/";
            }
        } catch (Exception e) {
            throw new RuntimeException("获取ClassPath路径出错");
        }
        if (!new File(classPath).exists()) {
            new File(classPath).mkdirs();
        }
        return classPath;
    }

    public static String getClassPath() throws Exception {

        String keyfilePath = URLDecoder.decode(Main.class.getProtectionDomain().
                getCodeSource().getLocation().getFile(), "UTF-8");
        keyfilePath = keyfilePath.replaceAll("\\\\", "/");
        File temp = new File(keyfilePath);
        if (temp.isFile() && keyfilePath.endsWith("jar") == true) {
            keyfilePath = keyfilePath.substring(0, keyfilePath.lastIndexOf("/")) + "/";
        } else if (keyfilePath.indexOf("classes") != -1) {
            keyfilePath = keyfilePath.substring(0, keyfilePath.indexOf("classes") + 7) + "/";
        }
        return keyfilePath;
    }

}
